package com.zat.mes.andrmes.dofun.main.vm

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import com.love.zywanandroid.retrofit.RetrofitManager
import com.love.zywanandroid.retrofit.api.ApiService
import com.zat.mes.andrmes.R
import com.zat.mes.andrmes.data.BaseResponse
import com.zat.mes.andrmes.data.BindMachineBean
import com.zat.mes.andrmes.data.MathineBean
import com.zat.mes.andrmes.retrofit.callback.MesCallBack
import okhttp3.RequestBody
import org.json.JSONObject
import retrofit2.Call
import retrofit2.Response

/**
 * @author zy
 * @date :2020-02-20 09:58
 * @description:绑定设备model
 */
class BindMachineModel(application: Application) : AndroidViewModel(application) {

    /**
     * 绑定设备
     */
    private val bindData: MutableLiveData<String> = MutableLiveData()
    /**
     * 绑定设备列表
     */
    private val bindDataList: MutableLiveData<List<BindMachineBean>> = MutableLiveData()
    /**
     * 解除绑定
     */
    private val delBindData: MutableLiveData<String> = MutableLiveData()

    /**
     * mac
     */
    private val macData: MutableLiveData<MathineBean> = MutableLiveData()

    fun getBindData(): MutableLiveData<String> {
        return bindData
    }
    fun getBindDataList(): MutableLiveData<List<BindMachineBean>> {
        return bindDataList
    }
    fun getDelBindData(): MutableLiveData<String> {
        return delBindData
    }
    fun getMathine():MutableLiveData<MathineBean>{
        return macData
    }

    /**
     * 绑定设备
     */
    fun loadBind(empId: Long, empName: String, qrCode: String?) {
        val jsonObject = JSONObject()
        jsonObject.put("empId", empId)
        jsonObject.put("empName", empName)
        jsonObject.put("QRcode", qrCode)
        val requestBody: RequestBody =
            RequestBody.create(RetrofitManager.JOSNTYPE, jsonObject.toString())
        RetrofitManager.apiService(ApiService::class.java)
            .bindMachine(requestBody)
            .enqueue(object : MesCallBack<BaseResponse<String>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<String>>?,
                    response: Response<BaseResponse<String>>?
                ) {
                    if (response?.body()?.success != null) {
                        if (response?.body()?.success!!) {
                            bindData.postValue(getApplication<Application>().getString(R.string.bind_success))
                        } else {
                            bindData.postValue(getApplication<Application>().getString(R.string.bind_fail))
                        }
                    } else {
                        bindData.postValue(getApplication<Application>().getString(R.string.net_request_fail))
                    }
                }

            })
    }

    /**
     * 获取绑定设备列表
     */
    fun loadBindList(empId: String) {

        RetrofitManager.apiService(ApiService::class.java)
            .bindMachineList(empId)
            .enqueue(object : MesCallBack<BaseResponse<List<BindMachineBean>>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<List<BindMachineBean>>>?,
                    response: Response<BaseResponse<List<BindMachineBean>>>?
                ) {
                    bindDataList.postValue(response?.body()?.result)
                }

            })
    }

    /**
     * 解除绑定
     */
    fun delBind(bindId: Long) {

        RetrofitManager.apiService(ApiService::class.java)
            .delBindMachine(bindId)
            .enqueue(object : MesCallBack<BaseResponse<String>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<String>>?,
                    response: Response<BaseResponse<String>>?
                ) {
                    if (response?.body()?.success != null) {
                        if (response?.body()?.success!!) {
                            delBindData.postValue(getApplication<Application>().getString(R.string.unbind_success))
                        } else {
                            delBindData.postValue(getApplication<Application>().getString(R.string.unbind_fail))
                        }
                    } else {
                        delBindData.postValue(getApplication<Application>().getString(R.string.net_request_fail))
                    }
                }

            })
    }

    fun selectMacByQr(qrCode:String?){
        RetrofitManager.apiService(ApiService::class.java)
            .getSysMacByQrcode(qrCode)
            .enqueue(object : MesCallBack<BaseResponse<MathineBean>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<MathineBean>>?,
                    response: Response<BaseResponse<MathineBean>>?
                ) {
                   return macData.postValue(response?.body()?.result)
                }

            })
    }

    /**
     *插入机器预警
     */
    fun insertMachineWarn(macNo: String?) {
        val jsonObject = JSONObject()
        jsonObject.put("MacNo", macNo)
        val requestBody: RequestBody =
            RequestBody.create(RetrofitManager.JOSNTYPE, jsonObject.toString())
        RetrofitManager.apiService(ApiService::class.java)
            .inserMachineWarn(requestBody)
            .enqueue(object : MesCallBack<BaseResponse<String>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<String>>?,
                    response: Response<BaseResponse<String>>?
                ) {
                    if (response?.body()?.success != null) {
                        if (response?.body()?.success!!) {
                            delBindData.postValue("预警成功")
                        } else {
                            delBindData.postValue("预警失败")
                        }
                    } else {
                        delBindData.postValue("请求失败")
                    }
                }

            })
    }

}